//
//  InquiryPriceInfoViewController.m
//  NewApp
//
//  Created by  on 12-7-9.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "InquiryPriceInfoViewController.h"
#import "InquiryPriceSearchResultViewController.h"
#import "Constants.h"
#import "ShoppingCarViewController.h"

@interface InquiryPriceInfoViewController(Private)

/**
 *  ToolView confirm button pressed
 */
- (void) toolViewConfirmButtonPressed;

@end

@implementation InquiryPriceInfoViewController
@synthesize toolView;

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void) dealloc {
    [toolView release];
    [super dealloc];
}

- (void) loadInlineInfos {
    [self.engine inquiryPriceInfosWithID:self.ID];
}

- (void) buildUserInterface {
    [super buildUserInterface];
    
    NAToolView  *tv = [[NAToolView alloc] initWithFrame:CGRectMake(10.f, self.imageView.frame.size.height + 20.f, 300.f, 44.f)];
    self.toolView = tv;
    tv.countTextfield.delegate = self;
    [tv.confirmButton addTarget:self action:@selector(toolViewConfirmButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:self.toolView];
    
    self.textView.frame = CGRectMake(10.f, self.toolView.frame.origin.y + self.toolView.frame.size.height + 10.f,self.textView.frame.size.width , self.textView.frame.size.width);
    [tv release];
}

- (void) searchButtonDidPressed {
    if (_searchBar.keywordTextfield.text.length == 0) 
        return;
    self.title = @"返回";
    InquiryPriceSearchResultViewController *viewCtrl = [[InquiryPriceSearchResultViewController alloc] initWithID:nil keyword:_searchBar.keywordTextfield.text];
    [self.navigationController pushViewController:viewCtrl animated:YES];
    [viewCtrl release];
}

- (void) requestDidFinished:(RequestEngine *)reqEngine withObject:(id)object {
    [super requestDidFinished:reqEngine withObject:object];
    
    if ([object isKindOfClass:[NSDictionary class]]) {
        NSDictionary *dic = (NSDictionary *) object;
        [dic retain];
        
        NSArray *objects = [dic objectForKey:@"data"];
        if (objects.count > 0) {
            NSDictionary *dataDic = [[dic objectForKey:@"data"] objectAtIndex:0];
            if (dataDic != nil) {
                
                //Retrieve text
                NSString *itemName = [dataDic objectForKey:kItemName];
                if (itemName != nil && itemName.length > 0) { 
                    [self.imageView setImageDescription:itemName];
                }
                
                NSString *itemNo = [dataDic objectForKey:kItemNo];
                if (itemNo != nil && itemNo.length > 0) { 
                    self.toolView.itemNoLabel.text = [NSString stringWithFormat:@"型號:%@",itemNo];
                }
                
                NSString *price = [dataDic objectForKey:kItemPrice];
                if (price != nil && price.length > 0) { 
                    self.toolView.priceLabel.text = [NSString stringWithFormat:@"價格:%@",price];
                }
                
                NSString *sprice = [dataDic objectForKey:kItemSprice];
                if (sprice != nil && sprice.length > 0) { 
                    if (self.toolView.priceLabel.text.length == 0) {
                        self.toolView.priceLabel.text = [NSString stringWithFormat:@"特價:%@",sprice];
                    }else {
                        self.toolView.priceLabel.text = [NSString stringWithFormat:@"%@ 特價:%@",self.toolView.priceLabel.text,sprice];
                    }
                    
                }
                
                
            }
            
            [dic release];
            
        }
    }
    
}

#pragma mark -
#pragma mark - UITextFieldDelegate methods

- (BOOL) textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    
    return YES;
}

- (void) textFieldDidBeginEditing:(UITextField *)textField {
    [self.scrollView setContentOffset:CGPointMake(0.f, self.toolView.frame.origin.y) animated:YES];
}

- (void) textFieldDidEndEditing:(UITextField *)textField {
    [self.scrollView setContentOffset:CGPointMake(0.f, 0.f)];
}

#pragma mark -
#pragma mark - Event handlers

- (void) toolViewConfirmButtonPressed {
    
    BOOL bValidated = YES;
    
    if (self.toolView.countTextfield.text.length == 0) {
        bValidated = NO;
    }
    
    if ([self.toolView.countTextfield.text intValue] == 0) {
        bValidated = NO;
    }
    
    if (!bValidated) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" 
                                                        message:@"請輸入正確的整數值" delegate:nil
                                              cancelButtonTitle:@"確定" 
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }
    
    
    NSMutableString *inquireList = [[NSMutableString alloc] init];
    [inquireList appendString:([[NSUserDefaults standardUserDefaults] objectForKey:kInquirePriceListKey] == nil) ? @"" : [[NSUserDefaults standardUserDefaults] objectForKey:kInquirePriceListKey]];
    
    if (inquireList.length == 0) {
        [inquireList appendFormat:@"%@,%@",self.ID,self.toolView.countTextfield.text];
    }else {
        [inquireList appendFormat:@"x%@,%@",self.ID,self.toolView.countTextfield.text];
    }
    
    [[NSUserDefaults standardUserDefaults] setObject:inquireList forKey:kInquirePriceListKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    ShoppingCarViewController *vctrl = [[ShoppingCarViewController alloc] init];
    vctrl.pageType = PAGE_TYPE_QUERY;
    vctrl.selectProducts = [inquireList retain];
    self.title = @"返回";
    [self.navigationController pushViewController:vctrl animated:YES];
    [vctrl release];
}



@end
